package uy.edu.um.ltic.client.communication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import uy.edu.um.ltic.client.gui.VentanaPrincipal;

public class ClientSocket {
	
	
	public static void connect(String sUser, String sPassword){
		try {
			BufferedReader bR;
			PrintWriter pW;
			Socket oSocket = new Socket(ServerIp.sIP, 7899);
			//System.out.println("hasta aca");
			
			bR = new BufferedReader(new InputStreamReader(
					oSocket.getInputStream()));
			pW = new PrintWriter(oSocket.getOutputStream(), true);
			
			while (true) {
				//System.out.println("leiiant");
				String sIndConf = bR.readLine();
				//System.out.println(sIndConf);
				if (sIndConf.equals("refrChatConf")){
					Integer iDConf = new Integer(bR.readLine());
					//System.out.println(iDConf + sIndConf);
					VentanaPrincipal.getInstance().refreshVentanaChatConferencia(iDConf);
				}else if (sIndConf.equals("refrChat")){
					String nEmpl = bR.readLine();
					//System.out.println(nEmpl + sIndConf);
					VentanaPrincipal.getInstance().actualizarUltimasConversaciones(nEmpl);
					
				}else if (sIndConf.equals("remConf")){
					String iDConf = bR.readLine();
					//System.out.println(iDConf + sIndConf);
				}else if (sIndConf.equals("startConf")){
					String iDConf = bR.readLine();
					//System.out.println(iDConf + sIndConf);
					VentanaPrincipal.getInstance().abrirVentanaChatConferencia(new Integer(iDConf));
					
				}else if (sIndConf.equals("Quienso")){
					pW.write(sUser +"\n");
					pW.write(sPassword+"\n");
					pW.flush();
				}
			}

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static void inicializarSocket(final String sUser, final String sPassword) {
		
			Thread oThread = new Thread(new Runnable() {
				
				public void run() {

					ClientSocket.connect(sUser, sPassword);
					
				}

			});
			oThread.start();
			
			
			
		}
		
	}

